package com.example.demo.Entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import sun.misc.BASE64Encoder;

import javax.persistence.Entity;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

@Data
public class ReturnBook {
    private String ISBN;
    private String Title;
    private String Author;
    private String CategoryId;
    private String Pages;
    private String Words;
    private String Publisher;
    private String Price;
    private String TotalCount;
    //private String PictureUrl;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private String ActionDate;
    private String Introduction;
    private String PictureBase;

    public static  ReturnBook trans(Book book){
        ReturnBook returnBook = new ReturnBook();
        returnBook.setISBN(book.getISBN());
        returnBook.setTitle(book.getTitle());
        returnBook.setAuthor(book.getAuthor());
        returnBook.setCategoryId(String.valueOf(book.getCategoryId()));
        returnBook.setPages(String.valueOf(book.getPages()));
        returnBook.setWords(String.valueOf(book.getWords()));
        returnBook.setPublisher(book.getPublisher());
        returnBook.setPrice(String.valueOf(book.getPrice()));
        returnBook.setTotalCount(String.valueOf(book.getTotalCount()));
    //    returnBook.setPictureUrl(book.getPictureUrl());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        returnBook.setActionDate(sdf.format(book.getActionDate()));
        returnBook.setIntroduction(book.getIntroduction());
    //    File file = new File(book.getPictureUrl());
    //    returnBook.setPictureCode(file2Base64(file));
        returnBook.setPictureBase(book.getPictureBase());
        return returnBook;
    }
    public static String file2Base64(File file) {
        InputStream in = null;
        byte[] data = null;        // 读取文件字节数组
        try {
            in = new FileInputStream(file);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        // 返回 Base64 编码过的字节数组字符串
        return encoder.encode(data);
    }
}
